<?php
namespace app\mobile\controller;

use app\mobile\service\CompanyService;
use app\mobile\service\ExportService;
use app\admin\service\DownsizingService;
use library\Controller;
use think\Db;

/**
 * 减员办理
 * Class Downconfig
 * @package app\mobile\controller
 */

class Downconfig extends Base
{
	/**
     * 减员确认列表
     */
    public function index()
    {
    	$where[] = ['d.company_id','eq',$this->company['id']];
    	$where[] = ['d.is_deleted','eq',0];
        $status = $this->request->get('status');
        if($status){
            $where[] = ['d.status','eq',$status];
        }else{
        	 $where[] = ['d.status','in',[1,2,3]];
        }
		$search =  $this->request->get('search');
		if($search){
			$where[] = ['d.code|i.name|i.id_number','like','%'.$search.'%'];
		}
        $list = Db::name('SystemDownsizingInsurance')
        		->alias('d')
				->join('system_increase i','d.increase_id = i.id')
        		->where($where)
        		->field('d.id,d.code,d.increase_id,d.end_date,d.status,d.confirm_date,d.insurance,i.name,i.id_number,i.mobile')
				->order('d.id desc')
    			->limit(10)
    			->select();
      	$this ->list = $list; 	
      	$this ->insurance = get_option('insurance');
      	$this ->total = Db::name('SystemDownsizingInsurance')->where(['company_id'=>$this->company['id'],'is_deleted'=>0,'status'=>3])->count();
        $this->fetch();
    }
    
    
    /**
     * 减员确认列表ajax
     */
    public function page_list()
    {
    	$page = $this->request->post('page');
    	if(!is_numeric($page)){
			$this->error('参数错误');
		}
		$minpage = ($page-1)*10;
		$where[] = ['d.company_id','eq',$this->company['id']];
    	$where[] = ['d.is_deleted','eq',0];
        $status = $this->request->post('status');
        if($status){
            $where[] = ['d.status','eq',$status];
        }else{
        	 $where[] = ['d.status','in',[1,2,3]];
        }
		$search =  $this->request->post('search');
		if($search){
			$where[] = ['d.code|i.name|i.id_number','like','%'.$search.'%'];
		}
        $list = Db::name('SystemDownsizingInsurance')
        		->alias('d')
				->join('system_increase i','d.increase_id = i.id')
        		->where($where)
        		->field('d.id,d.code,d.increase_id,d.end_date,d.status,d.confirm_date,d.insurance,i.name,i.id_number,i.mobile')
				->order('d.id desc')
    			->limit($minpage,10)
    			->select();
      	$this ->list = $list;
      	if($list){
    		foreach($list as $k=>$v){
	    		$list[$k]['insurance'] = get_option('insurance')[$v['insurance']]; 		
	    	}
    	} 	
        $this->success('获取成功',$list);	
		
    }
    
    
     /**
     * 减员编辑
     */
    public function edit()
    {
    	if($this->request->isPost()){
    		$data = $this->request->post();
			trim_array($data);
			$downsizing = Db::name('system_downsizing_insurance')->where(['company_id'=>$this->company['id'],'id'=>$data['id'],'is_deleted'=>0])->find();
			if(!$downsizing){
				$this->error('暂无员工信息');
			}	
			$person = Db::name('system_person')->where(['id'=>$downsizing['person_id'],'company_id'=>$this->company['id']])->find();
			if(!$person || in_array($person['status'], [2,3])){
				$this->error('该员工不在职，无法减少险种');
			}			
			$insurance_result = Db::name('system_insurance_result')->where(['person_id'=>$person['id'],'insurance'=>$downsizing['insurance'],'status'=>1,'increase_id'=>$downsizing['increase_id']])->count();
			if(!$insurance_result){
				$this->error('该险种不在职，无法减少险种');
			}
			$find = Db::name('system_downsizing')->where(['person_id'=>$person['id'],'status'=>[1,3],'is_deleted'=>0])->find();
			if($find){
				$this->error('该员工正在办理减员，无法减少险种');
			}
			$downsizing_insurance = Db::name('system_downsizing_insurance')->where(['increase_id'=>$downsizing['increase_id'],'insurance'=>$downsizing['insurance'],'is_deleted'=>0])->find();
			if($downsizing_insurance['status'] == 1){
				$this->error('该险种正在办理减少，请勿重复提交');
			}
			if($downsizing_insurance['status'] == 2){
				$this->error('该险种不在职，无法减少险种');
			}		
			Db::name('system_downsizing_insurance')->where(['company_id'=>$this->company['id'],'id'=>$data['id'],'is_deleted'=>0])->update(['end_date'=>$data['end_date'],'account_period'=>$data['account_period'],'status'=>1]);
			$this->success('努力提交中......', url('mobile/downconfig/subSuccess'));
    	}else{
    		$id = $this->request->get('id');
			$downsizing = Db::name('system_downsizing_insurance')->where(['company_id'=>$this->company['id'],'id'=>$id,'is_deleted'=>0])->find();
			if(!$downsizing){
				$this->redirect('mobile/publicot/transition', ['info'=>'暂无员工信息','type'=>'6']);
			}
			$increase = Db::name('system_increase')->where(['id'=>$downsizing['increase_id']])->find();			
			$this->increase = $increase;
			$this->downsizing = $downsizing;			
			$this->insurances = get_option('insurance');
			$date = date('Y-m-d',time());
			$nextmonth = after_month($date,1);
			$this->nextmonth = $nextmonth;
    	}
        $this->fetch();
    }
	
	
	/**
     * 减员查看
     */
    public function view()
    {
    	$id = $this->request->get('id');
		$downsizing = Db::name('system_downsizing_insurance')->where(['company_id'=>$this->company['id'],'id'=>$id,'is_deleted'=>0])->find();
		if(!$downsizing){
			$this->redirect('mobile/publicot/transition', ['info'=>'暂无员工信息','type'=>'6']);
		}
		$increase = Db::name('system_increase')->where(['id'=>$downsizing['increase_id']])->find();
		$this->downsizing = $downsizing;
		$this->increase = $increase;
		$this->insurances = get_option('insurance');
        $this->fetch();
    }
    
    
    /**
     * 减员成功
     */
    public function subSuccess()
	{
		$this->fetch();
	}
	
	
	/**
     * 导出
     */
    public function export()
    {
        $data = $this->request->param();
    	$ids = explode(',', $data['id']);
		$list = ExportService::export_downconfig($ids,$this->company['id']);
    	toexcel($list['filename'],$list['titles'],$list['list']);  
    }
    
}
